package com.sm.tools;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sm.R;
import com.sm.bo.Student;

public class MyCustomStudentAdapter extends ArrayAdapter<Student> {

	public ArrayList<Student> studentList;

	ViewHolder holder;
	
	private int selectedIndex = -1;

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public MyCustomStudentAdapter(Context context, int textViewResourceId,
			ArrayList<Student> studentList) {
		super(context, textViewResourceId, studentList);
				
		this.studentList = new ArrayList<Student>();
		this.studentList.addAll(studentList);
	}

	private class ViewHolder {
		TextView code;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		holder = null;
		Log.v("ConvertView", String.valueOf(position));

		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.studentlist_view, null);

			holder = new ViewHolder();
			holder.code = (TextView) convertView.findViewById(R.id.tv_Student_Name_SL_W);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (selectedIndex == position) {
			holder.code.setBackgroundColor(Color.GREEN);
		} else {

		}
		Student student = studentList.get(position);
//		holder.code.setText(" (" + student.getName() + ")");
		holder.code.setText(student.getName() + " (--UUID--)");
		return convertView;
	}
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
}